package com.code.user.config;

import com.code.common.model.ResponseCode;
import com.code.common.model.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
import org.springframework.stereotype.Component;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: Yan
 * @date: 2023/4/14
 */
@Slf4j
@Component
public class OauthException implements WebResponseExceptionTranslator {

    @Override
    public ResponseEntity translate(Exception e) throws Exception {
        log.error("oauth 异常：{}", e.getMessage());

        ResultVO<Object> resultVO = ResultVO.ERROR(ResponseCode.SERVER_ERROR);

        if (e instanceof AccessDeniedException) {
            resultVO = ResultVO.ERROR(ResponseCode.NO_ACCESS);
        }

        return ResponseEntity.ok(resultVO);
    }

}
